package com.meichis.mcsappframework.utils;

/**
 * Created by pei on 2016/12/9.
 * 数据类型转换，所有异常返回0
 */

public class ParseUtil {

    public static int parseInt(Object object) {
        if (object == null || object.toString().trim().equals(""))
            return 0;
        double number;
        try {
            number = Double.parseDouble(object.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
        if (number > Integer.MAX_VALUE || number < Integer.MIN_VALUE) {
            return 0;
        }
        return (int) number;
    }

    public static float parseFloat(Object object) {
        if (object == null || object.toString().trim().equals(""))
            return 0f;
        double number;
        try {
            number = Double.parseDouble(object.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0f;
        }
        if (number > Float.MAX_VALUE || number < Float.MIN_VALUE) {
            return 0;
        }
        return (float) number;
    }

    public static double parseDouble(Object object) {
        if (object == null || object.toString().trim().equals(""))
            return 0d;
        try {
            return Double.parseDouble(object.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0d;
        }
    }
}
